package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.BpmnInfoQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpmnInfoServiceImpl implements IBpmnInfoService {

    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;

    @Autowired
    private RepositoryService repositoryService;
    @Override
    public TablePageInfo<BpmnInfo> query(BpmnInfoQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BpmnInfo>(bpmnInfoMapper.selectForList(qo));
    }

    @Override
    public String upload(MultipartFile file) throws IOException {

        //获取后缀名
        String extension = FileUploadUtils.getExtension(file);
        if (!"bpmn".equals(extension)) {
            throw new BusinessException("文件格式不正确");
        }

        //保存文件
        String profile = SystemConfig.getProfile();
        String path = FileUploadUtils.upload(profile, file);

        return path;
    }

    @Override
    @Transactional
    public void deploy(String path, String bpmnType, String info) throws IOException {

        //拿到文件
        File file = new File(SystemConfig.getProfile(), path);
        InputStream fileInputStream = new FileInputStream(file);
        //进行流程部署,拿到资源service,使用addInputStream(),在这之前要拿到文件
        Deployment deploy = repositoryService.createDeployment().addInputStream(path, fileInputStream).deploy();
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
        fileInputStream.close();
        BpmnInfo bpmnInfo = new BpmnInfo();
        bpmnInfo.setDeploymentId(deploy.getId());
        bpmnInfo.setDeployTime(deploy.getDeploymentTime());
        bpmnInfo.setBpmnType(bpmnType);
        bpmnInfo.setInfo(info);
        bpmnInfo.setActProcessId(definition.getId());
        bpmnInfo.setActProcessKey(definition.getKey());
        bpmnInfo.setBpmnName("服务套餐审核流程");
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public void delete(Long id) {
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(id);
        //删除把bpmn表中的数据
        bpmnInfoMapper.deleteByPrimaryKey(id);

        //使用定义id把去查询定义id然后把定义删除
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(bpmnInfo.getDeploymentId()).singleResult();
        repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true);

        //删除部署文件
        File file = new File(SystemConfig.getProfile(), processDefinition.getResourceName());
        if (file.exists()) {
            file.delete();
        }
    }

    @Override
    public InputStream readResource(String deploymentId, String type) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        InputStream resourceAsStream = null;
        if ("xml".equals(type)){
             resourceAsStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
        }else {
            DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
            resourceAsStream = defaultProcessDiagramGenerator.generateDiagram(
                    repositoryService.getBpmnModel(processDefinition.getId()),
                    "楷体",
                    "楷体",
                    "楷体");
        }

        return resourceAsStream;
    }

    @Override
    public List<BpmnInfo> queryByType(String car_package) {

        return bpmnInfoMapper.queryByType(car_package);
    }
}
